const http = require('http');
const fs = require('fs');
http.createServer((req, res) => {
    if (req.url === '/stream') {
        res.writeHead(200, {
            "Content-Type": "text/event-stream",
            "Connection": "keep-alive"
        })
        res.write("data: Connected\n\n")
        let count = 1;
        let interval = setInterval(() => {
            res.write("data: Message" + count + "\n\n")
        },1000);
        req.on('close', () => {
            clearInterval(interval)
        })
    }else if(req.url === '/'){
        const result = fs.readFileSync('./sse.html','utf-8');
        res.writeHead(200, {
            "Content-Type": "text/html",
            "Connection": "keep-alive"
        })
        res.end(result)
    }
}).listen(4000);